// pages/component/homes/purchase/buycardpay/buycardpay.js
var that;
var dataManager = require('../../../../../utils/dataManager.js');
var GlobalData = require('../../../../../utils/GlobalData.js');
var Util = require('../../../../../utils/util.js');
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    shopcarArr: GlobalData.shopcar_1,
    price: 0,
    isShow: false,
    hidden: true

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    dataManager.removeBtnObserver("buycardpay")
    this.setData({
      hidden: true
    })
    
    let pri = 0;
    for (let i = 0; i < GlobalData.shopcar_1.length; i++) {
      pri += GlobalData.shopcar_1[i].pro.MemberPrice * GlobalData.shopcar_1[i].count;

    }
    pri = Math.round(pri * 100) / 100;
    this.setData({
      price: pri,
      shopcarArr: GlobalData.shopcar_1
    })
  },
  // 点击确认购买按钮
  onOk: function () {
    var that = this;
    this.setData({
      isShow: true
    })
    // wx.showModal({
    //   content: "支付完成后是否帮您取出房卡？",
    //   // showCancel: true,
    //   cancelText: "存放卡库",
    //   confirmText: "全部取出",
    //   success: function (res) {
    //     //全部取出
    //     if (res.confirm) {
    //       GlobalData.isTakeCard = 1;
    //       that.onPayClick();
    //     } else if (res.confirm == 0) {
    //       // 存放卡库

    //       GlobalData.isTakeCard = 0;
    //       that.onPayClick();
    //     }

    //   }
    // })
  },
  onSaveCarddepot: function () {
    this.setData({
      isShow: false
    })
    GlobalData.isTakeCard = 0;
    this.onPayClick();
  },
  onTakeOut: function () {
    this.setData({
      isShow: false
    })
    GlobalData.isTakeCard = 1;
    this.onPayClick();
  },
  onClose: function () {
    this.setData({
      isShow: false
    })
  },
  onPayClick: function () {
    var that = this;
    let products = [];
    let totalCount = 0;

    this.setData({
      hidden: false
    })

    for (let i = 0; i < GlobalData.shopcar_1.length; i++) {
      products[i] = {};
      products[i]["sysno"] = GlobalData.shopcar_1[i].pro.SysNo;
      products[i]["count"] = GlobalData.shopcar_1[i].count;
      totalCount += GlobalData.shopcar_1[i].count;
    }
    var jsonstr = JSON.stringify(products);
     let url = app.getRequestUrl(app.getConfig().isFormal);
    wx.request({
      url: url,
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      method: "POST",
      data: Util.json2Form({ method: "addorder", token: GlobalData.token, "ordertype": 0, "paytype": 0, "products": jsonstr, "istakecard": GlobalData.isTakeCard, source: "SMAPP" }),
      complete: function (res) {
        let pkg = res.data;
        if (pkg && pkg.errcode == 0) {
          //购买成功清空购物车
          // GlobalData.shopcar_1 = [];
          //清空购卡记录
          // GlobalData.BuyCardRecord = null;
          GlobalData.order = {};
          GlobalData.order["orderno"] = pkg.data.orderno;
          GlobalData.order["money"] = pkg.data.totalprice;
          GlobalData.order["sysno"] = pkg.data.sysno;
          GlobalData.order["totalcount"] = totalCount;
          GlobalData.order["rechargetype"] = "购买房卡";
          GlobalData.order["owntype"] = false;
          if (!GlobalData.order["time"])
            GlobalData.order["time"] = that.getDate();
          GlobalData.PayReason = 2;
          // this.router.navigateByUrl('payment/buycardpay' + GlobalData._random
          dataManager.OnRequestPost({ method: "getpaytype", "type": 2 }, that.onProcessgetpaytype, false, that.onError);
        } else {
          that.setData({
            hidden: true
          })
          app.onTip(pkg.errmsg)
        }
      }
    })

  },
  onError: function () {
    this.setData({
      hidden: true
    })
  },
  /**获取支付类型 */
  onProcessgetpaytype: function (data) {
    let arr = [];
    for (let i = 0; i < data.length; i++) {
      arr[i] = {};
      arr[i] = GlobalData.clone(data[i]);
    }
    GlobalData.PayType[GlobalData.PayReason] = arr;

    // if (dataManager.isClick("payment")) {
    //   return
    // } else {
    //   dataManager.btnObserver("payment")
    this.setData({
      hidden: true
    })
    wx.redirectTo({
      url: '../../purchase/payment/payment',
    })
    // }


  },
  getDate: function () {
    var d = new Date();
    var year = d.getFullYear();
    var month = d.getMonth() + 1;
    var date = d.getDate();
    var day = d.getDay();
    var hours = d.getHours();
    var minutes = d.getMinutes();
    var seconds = d.getSeconds();
    var ms = d.getMilliseconds();
    var curDateTime = year;
    if (month > 9)
      curDateTime = curDateTime + "-" + month;
    else
      curDateTime = curDateTime + "-0" + month;
    if (date > 9)
      curDateTime = curDateTime + "-" + date;
    else
      curDateTime = curDateTime + "-0" + date;
    curDateTime = curDateTime + " ";
    if (hours > 9)
      curDateTime = curDateTime + "" + hours;
    else
      curDateTime = curDateTime + "0" + hours;
    if (minutes > 9)
      curDateTime = curDateTime + ":" + minutes;
    else
      curDateTime = curDateTime + ":0" + minutes;
    if (seconds > 9)
      curDateTime = curDateTime + ":" + seconds;
    else
      curDateTime = curDateTime + ":0" + seconds;
    return curDateTime;
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  // onShareAppMessage: function () {

  // }
})